layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
        elem: '#startTime',
        value:new Date()
    });

    laydate.render({
        elem: '#endTime',
        value:new Date()
    });
    /**
     * 恒安临时车超额审批标准管理
     */
    var FiHaTempCarAppStandExa = {
        tableId: "fiHaTempCarAppStandExaTable"
    };

    /**
     * 初始化表格的列
     */
    FiHaTempCarAppStandExa.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'operateName', title: '项目', style:'font-weight: 600;'},
            {field: 'startDate', title: '发车日期', style:'font-weight: 600;', sort: true},
            {field: 'unitType', title: '客户类型', style:'font-weight: 600;', sort: true},
            {field: 'dispatchNumber', title: '运输单号', style:'font-weight: 600;',templet: '#dispatchNumberTpl'},
            {field: 'carnumber', title: '车牌号', style:'font-weight: 600;', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'siji', title: '司机', style:'font-weight: 600;', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'sijidianhua', title: '联系方式', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'chexing', title: '车型', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'kilometer', title: '最远距离', style:'background-color: #dbf1e1;font-weight: 600;', sort: true},
            {field: 'zhunzailiang', title: '准载方数', style:'background-color: #dbf1e1;font-weight: 600;', sort: true},
            {field: 'toovolume', title: '总方数', style:'background-color: #dbf1e1;font-weight: 600;', sort: true},
            {field: 'carrierName', sort: true, title: '承运商', style:'font-weight: 600;'},
            {field: 'lineName', sort: true, title: '线路', style:'font-weight: 600;'},
            {field: 'matchRs', title: '结算状态', style:'font-weight: 600;'},
            {field: 'freightMoney', title: '运费￥', style:'font-weight: 600;',totalRow: true,sort: true,},
            {field: 'bz', title: '标准运费￥', style:'font-weight: 600;',sort: true,},
            {field: 'excRate', title: '限定超额比%', style:'font-weight: 600;',sort: true,},
            {field: 'actualExcess', title: '实际超额比%', style:'font-weight: 600;',sort: true,}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FiHaTempCarAppStandExa.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['matchRs'] = $("#matchRs").val();
        table.reload(FiHaTempCarAppStandExa.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    FiHaTempCarAppStandExa.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/fiHaTempCarAppStand/add';
    };

    /**
     * 导出excel按钮
     */
    FiHaTempCarAppStandExa.exportExcel = function () {
        var checkRows = table.checkStatus(FiHaTempCarAppStandExa.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    FiHaTempCarAppStandExa.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/fiHaTempCarAppStand/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FiHaTempCarAppStandExa.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/fiHaTempCarAppStand/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(FiHaTempCarAppStandExa.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };


    FiHaTempCarAppStandExa.onShenhe = function () {
        debugger
        var checkRows = table.checkStatus(FiHaTempCarAppStandExa.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要审核的数据");
        } else {
            var ids = checkRows.data.map(function(item) {
                return item.id;
            });
            var operation = function () {

                var ajax = new $ax(Feng.ctxPath + "/fiTempCarAppStandExa/shenhe", function (data) {
                    if(data.success){
                        Feng.success("操作成功!");
                        table.reload(FiHaTempCarAppStandExa.tableId);
                    }else{
                        Feng.error("操作失败!" + data.message + "!");
                    }
                }, function (data) {
                    Feng.error("操作失败!" + data.responseJSON.message + "!");
                });
                ajax.set("ids", ids.join(","));
                ajax.start();
            };
            Feng.confirm("是否对选择的记录发起审核?", operation);
        }
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FiHaTempCarAppStandExa.tableId,
        url: Feng.ctxPath + '/fiHaTempCarAppStand/approveList',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 130,
        cols: FiHaTempCarAppStandExa.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FiHaTempCarAppStandExa.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        FiHaTempCarAppStandExa.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        FiHaTempCarAppStandExa.exportExcel();
    });

    // 审核
    $('#btnShenhe').click(function () {
        FiHaTempCarAppStandExa.onShenhe();
    });

    // 工具条点击事件
    table.on('tool(' + FiHaTempCarAppStandExa.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            FiHaTempCarAppStandExa.openEditDlg(data);
        } else if (layEvent === 'delete') {
            FiHaTempCarAppStandExa.onDeleteItem(data);
        }
    });
});
